package com.wenli.graduate2022.wenliGreenShop.customizeComponents;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 自定义一个拦截器处理器，拦截没有权限的游客，这里就不用@Configuration加入配置类了，到时候我直接自定义一个WebMvcConfigurer,去替换springboot的默认配置功能
 * @author HuangHai
 * @date 2021/9/28 15:49
 */
public class LoginHandlerIntercept implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Object object= request.getSession().getAttribute("userLoginStatus");//已经登录的在session域里会有值
        if (object == null){
            request.setAttribute("tips","没有权限，请先登录！");
            System.out.println("拦截器已经启动");
            //转到登录页面
            request.getRequestDispatcher("/userLogin").forward(request,response);
            return false;
        }
        return true;  //否则就返回已经登录
    }
}
